﻿using System;
using System.Collections.Generic;

namespace Commands.Logging
{
    public class Log
    {
        public Log()
        {
            Tasks = new List<TaskMessage>();
        }

        public List<TaskMessage> Tasks
        {
            get;
            private set;
        }

        public void AddTask(string message)
        {
            Tasks.Add(new TaskMessage(message));
        }

        public TaskMessage GetLastTask()
        {
            return Tasks[Tasks.Count - 1];
        }

        internal void AddMessage(string format, params object[] list)
        {
            var message = string.Format(format, list);
            GetLastTask().AddMessage(message);
        }

        internal Message GetLastMessage()
        {
            var task = GetLastTask();
            return task.Messages[task.Messages.Count - 1];
        }

        public override string ToString()
        {
            string result = null;
            foreach (var taskMessage in Tasks)
                result += taskMessage.ToString();

            return result;
        }
    }

    public class TaskMessage
    {
        public TaskMessage(string message)
        {
            Messages = new List<Message>();

            AddMessage(message);
        }

        public List<Message> Messages;

        internal void AddMessage(string message)
        {
            Messages.Add(new Message(message));
        }

        public override string ToString()
        {
            string result = null;
            Messages.ForEach((message) => {result += message.ToString();});

            return result;
        }
    }

    public class Message
    {
        public Message(string message)
        {
            Text = message;
            Time = DateTime.Now;
        }

        public string Text
        {
            get;
            private set;
        }

        public DateTime Time { private set; get; }

        public override string ToString()
        {
            return string.Format("{0}: {1}", Time.ToString(), Text);
        }

    }
}
